package sk.fhi.server.guice;

import net.customware.gwt.dispatch.server.Dispatch;
import net.customware.gwt.dispatch.server.guice.GuiceStandardDispatchServlet;
import net.customware.gwt.dispatch.shared.Action;
import net.customware.gwt.dispatch.shared.ActionException;
import net.customware.gwt.dispatch.shared.Result;

import org.apache.commons.logging.Log;

import sk.fhi.shared.rpc.AbstractAction;

import com.google.inject.Inject;
import com.google.inject.Singleton;

@Singleton
public class DispatchServlet extends GuiceStandardDispatchServlet {

	private static final long serialVersionUID = 4895255235709260169L;

	private final Log logger;

	@Inject
	public DispatchServlet(final Dispatch dispatch, final Log logger) {
		super(dispatch);
		this.logger = logger;

	}

	@Override
	public Result execute(final Action<?> action) throws ActionException {
		try {
			logger.info("executing: "
					+ ((AbstractAction) action).getLogMessage());
			final Result res = super.execute(action);
			return res;
		} catch (final ActionException ae) {
			logger.error(ae.getMessage());
			ae.printStackTrace();
			throw ae;
		} catch (final Exception e) {
			logger.error("Unexpected exception: " + e.getMessage(), e);
			e.printStackTrace();
		}
		return null;
	}

}
